package service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import util.Utilities;
import vo.OrderListVO;
import vo.OrderVO;
import dao.BusinessOrderDAO;

public class BusinessOrderService {
	private BusinessOrderDAO businessOrderDAO;
	
	public void setBusinessOrderDAO(BusinessOrderDAO businessOrderDAO) {
		this.businessOrderDAO = businessOrderDAO;
	}

	public ArrayList<OrderListVO> selectAllBusinessOrderList(String member_id) throws SQLException{
	
		ArrayList<OrderListVO> list =  businessOrderDAO.selectAllOrderList(member_id);
		for(OrderListVO ovo : list){
			String time = ovo.getResult_time();
			if(time == null){
				time = Utilities.getNow();
			}
			ovo.setResult_time(Utilities.getDateTime(time));
		}
		return list;
	}
	
	public void updateAcceptOrder(String order_id, String member_id) throws SQLException{
		HashMap<String, String> id = new HashMap<String, String>();
		id.put("order_id", order_id);
		id.put("member_id", member_id);
		id.put("result_time", Utilities.getNow());
		businessOrderDAO.updateAcceptOrder(id);
	}
	
	public void updateTranslateOrder(String order_id, String member_id) throws SQLException{
		HashMap<String, String> id = new HashMap<String, String>();
		id.put("order_id", order_id);
		id.put("member_id", member_id);
		id.put("result_time", Utilities.getNow());
		businessOrderDAO.updateTranslateOrder(id);
	}
	
	public void updateCompleteOrder(String order_id, String member_id) throws SQLException{
		HashMap<String, String> id = new HashMap<String, String>();
		id.put("order_id", order_id);
		id.put("member_id", member_id);
		id.put("result_time", Utilities.getNow());
		businessOrderDAO.updateCompleteOrder(id);
	}
	
	public ArrayList<String> selectOrderId(String member_id) throws SQLException{
		return businessOrderDAO.selectOrderId(member_id);
	}
	
	public ArrayList<OrderVO> selectOrderVO(ArrayList<String> orderIds, String member_id) throws SQLException{
		ArrayList<OrderVO> orderVO = new ArrayList<OrderVO>();
		for(String order_Id : orderIds){
			HashMap<String, String> map = new HashMap<String, String>();
			OrderVO temp = new OrderVO();
			map.put("order_id", order_Id);
			map.put("member_id", member_id);
			temp = businessOrderDAO.selectOrderVOById(order_Id);
			temp.setList(businessOrderDAO.selectOrderListByOrderId(map));
			orderVO.add(temp);
		}
		
		return orderVO;
	}
	
	public ArrayList<OrderListVO> selectOrderListById(HashMap<String, String> map) throws SQLException{
		return businessOrderDAO.selectOrderListByOrderId(map);
	}
}
